﻿using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using GameView.Comment;
using GameView.Services;
using GameView.Services.Comment;
using GameView.Services.Dtos.ConsumProduct;
using GameView.Services.Dtos.Project;
using System.Collections.ObjectModel;

namespace GameView.ViewModels
{
    public partial class ConProStatisticsViewModel : ObservableObject
    {
        private List<ProjectWithGamesInfo> pgs = [];
        private readonly ProjectService _proService;
        private readonly ConsumProductService _service;
        public ConProStatisticsViewModel(ConsumProductService service, ProjectService pservice)
        {
            _service = service;
            _proService = pservice;
            LoadData().ConfigureAwait(false);
            SelectedOptionType = STypeOptions.First();
        }
        [ObservableProperty]
        private bool _isLoading = false;
        [ObservableProperty]
        private ObservableCollection<OptionType> _sTypeOptions = [new (100, "全部"), new (101, "大区统计"), new (102, "NPC统计"),
            new (103, "行会统计"), new (104, "玩家统计")];
        [ObservableProperty]
        [NotifyPropertyChangedFor(nameof(ProjectShow))]
        [NotifyPropertyChangedFor(nameof(GameShow))]
        [NotifyPropertyChangedFor(nameof(NpcShow))]
        [NotifyPropertyChangedFor(nameof(HhShow))]
        [NotifyPropertyChangedFor(nameof(WjShow))]
        private OptionType _selectedOptionType;
        [ObservableProperty]
        private ObservableCollection<string> _projectOptions = [];
        [ObservableProperty]
        [NotifyPropertyChangedFor(nameof(GameOptions))]
        private string _selectedProject;
        [ObservableProperty]
        private ObservableCollection<string> _gameOptions = [];
        [ObservableProperty]
        private string _hName = "";
        [ObservableProperty]
        private ConProQueryDto _queryDto = new();
        [ObservableProperty]
        private ConProStatisticsAll _statisticsAll = new();

        [ObservableProperty]
        private ObservableCollection<ConProStatisticsDto> _conProRecords = [];
        public bool ProjectShow => SelectedOptionType != null && SelectedOptionType.Id > 100;

        public bool GameShow => SelectedOptionType != null && SelectedOptionType.Id > 101;

        public bool NpcShow => SelectedOptionType != null && SelectedOptionType.Id == 102;
        public bool HhShow => SelectedOptionType != null && SelectedOptionType.Id == 103;
        public bool WjShow => SelectedOptionType != null && SelectedOptionType.Id == 104;

        [RelayCommand]
        private async Task SearchAsync()
        {
            IsLoading = true;
            await Task.Delay(200);
            try
            {
                await LoadFromApi();
            }
            catch (Exception ex)
            {
                GlobalMessage.Instance.ShowError($"数据加载错误：{ex.Message}");
            }
            finally
            { IsLoading = false; }
        }
        [RelayCommand]
        private async Task LoadData()
        {
            IsLoading = true;
            await LoadFromApi();
            var res = await _proService.GetAllAsync();
            pgs = res.Data;
            ProjectOptions.Clear();
            GameOptions.Clear();
            pgs ??= [];
            foreach (var data in pgs)
            {
                ProjectOptions.Add(data.Name);
            }
            if (pgs.Count >= 1)
            {
                foreach (var item in pgs.First().Games)
                    GameOptions.Add(item.Name);
            }
            IsLoading = false;
        }
        partial void OnSelectedOptionTypeChanged(OptionType value)
        {
            if (value == null)
            {
                return;
            }
            switch (value.Id)
            {
                case 100:
                    HName = "项目名称";
                    break;

                case 101:
                    HName = "大区名称";
                    break;

                case 102:
                    HName = "NPC名称";
                    break;

                case 103:
                    HName = "行会名称";
                    break;

                case 104:
                    HName = "玩家名称";
                    break;
            }
            QueryDto.Type = value.Id;
        }
        partial void OnSelectedProjectChanged(string value)
        {
            // 清空游戏选项
            GameOptions.Clear();
            // 根据选择的项目重新加载游戏选项
            if (!string.IsNullOrEmpty(value))
            {
                var nowProjet = pgs.FirstOrDefault(q => q.Name == SelectedProject);
                if (nowProjet == null || nowProjet.Games.IsNullOrEmpty())
                    return;
                foreach (var game in nowProjet.Games)
                {
                    GameOptions.Add(game.Name);
                }
            }
        }
        private async Task LoadFromApi()
        {
            IsLoading = true;
            await Task.Delay(200);
            try
            {
                ConProRecords.Clear();
                QueryDto.PageIndex = 10;
                QueryDto.PageSize = 10;

                QueryDto.ProjectName = SelectedProject;
                var res = await _service.StatisticsProCon(QueryDto);
                if (res.StatusCode != 200)
                {
                    GlobalMessage.Instance.ShowError(JSON.Serialize(res.Errors));
                    return;
                }

                if (res.Data.IsNullOrEmpty())
                    return;
                StatisticsAll = new(res.Data);
                foreach (var rr in res.Data)
                {
                    ConProRecords.Add(rr);
                }
            }
            catch (Exception ex)
            {
                GlobalMessage.Instance.ShowError($"数据加载错误：{ex.Message}");
            }
            finally
            { IsLoading = false; }
        }
    }
}